%%  hsplit.sty      dirty implementation of \hsplit  coded by Dian YIN
%%  Example:
%%           \setbox0=\hbox{In\-ter\-na\-tion\-ali\-za\-tion is the trend.}
%%           \hsplit0\to 4ex\into2
%%           \line{\hrulefill}
%%           \box2
%%           \line{\hrulefill}
%%           \box0
%%  Note:   The glue at the beginning of the \hbox will be suppressed!
%%  Hist:   07.2.14.  Coded according to inspiration from TeXbook App. D.
%%            \hsplit reformats the box in a \vbox with the given \hsize.
%%            The glues will be suppressed at linebreak. \hsplit adds
%%            \hsplit@linebreakskip at the place of linebreak. You can 
%%            change its content to a proper value when using.
%%          07.2.15.  Recoded \hsplit, changed the \hsize when reformatting
%%            to \wd\inputbox - \splitdimen, and puts a kern just before 
%%            the unhboxed \inputbox, the width of which is \wd\inputbox
%%            - 2\splitdimen, so that the first line after reformatting
%%            is the wanted result prefixed by the kerning. Another breaking
%%            at the beginning of the line removes the kerning and the glue
%%            at the start of the given \hbox if exists. \hsplit@linebreakskip
%%            is no more needed. The problem of hyphenation is properly solved.
%%          07.2.17.  Fixed bug of extra space added when calling \hsplit.
%%          07.2.23.  Fixed bug of lost glue in the \into box, \hsize=\maxdimen
\catcode`\@=11
\ifx\hsplit@defined\@undefined
\def\hsplit@defined{\relax}%
\newbox\hsplit@tmpbox
\newbox\hsplit@tmpboy
\newbox\hsplit@tmpboz
\newbox\hsplit@tmpbow
\newbox\hsplit@tmpbov
\newdimen\hsplit@tmpdim
\def\hsplit#1\to#2\into#3{%  \hsplit\boxnum\to\dimen\into\outboxnum
  \hsplit@tmpdim=#2%
  \multiply\hsplit@tmpdim by -1
  \advance\hsplit@tmpdim by \wd#1%
  \setbox\hsplit@tmpbox=\vbox{\hfuzz=\maxdimen\hbadness=\@M
    \hsize=\hsplit@tmpdim\nointerlineskip
    \global\hsplit@tmpdim=#2
    \global\multiply\hsplit@tmpdim by -2
    \global\advance\hsplit@tmpdim by \wd#1
    \noindent\kern\hsplit@tmpdim\unhbox#1}%
  %\showbox\hsplit@tmpbox
  \setbox\hsplit@tmpbox=\vbox{% get into internal vertical mode
    \unvbox\hsplit@tmpbox
    \hsplit@makehboxofhboxesandsplit
    \global\setbox\hsplit@tmpbow=\hbox{\unhbox\hsplit@tmpbow
      \hsplit@removehboxes\unskip\unpenalty}%
    \hbadness=\@M\hfuzz=\maxdimen
    \global\setbox\hsplit@tmpbov=\hbox to #2{\unhbox\hsplit@tmpbov\unskip}%
    % Now we've got the first line in \hsplit@tmpbov starting with a kern
    %\showbox\hsplit@tmpbow
  }%
  %\showbox\hsplit@tmpbov
  \setbox#1=\box\hsplit@tmpbow
  \setbox\hsplit@tmpbox=\vbox{\hbadness=\@M\hfuzz=\maxdimen
    \hsize=\maxdimen\nointerlineskip
    \noindent\break\unhbox\hsplit@tmpbov}%
  %\showbox\hsplit@tmpbox
  \setbox\hsplit@tmpbox=\vbox{% get into internal vertical mode
    \unvbox\hsplit@tmpbox
    \hsplit@makehboxofhboxesandsplit
    %\showbox\hsplit@tmpbov
    \global\setbox\hsplit@tmpbow=\hbox{\unhbox\hsplit@tmpbow
      \hsplit@removehboxes\unskip\unpenalty}%
    %\showbox\hsplit@tmpbow
  }%
  \setbox#3=\box\hsplit@tmpbow
}%
\def\hsplit@makehboxofhboxesandsplit{%
  \setbox\hsplit@tmpboy=\hbox{}%
  \loop
    \setbox\hsplit@tmpboz=\lastbox
    \ifhbox\hsplit@tmpboz
      \setbox\hsplit@tmpbow=\copy\hsplit@tmpboy
      \setbox\hsplit@tmpbov=\copy\hsplit@tmpboz
      \setbox\hsplit@tmpboy=\hbox{\box\hsplit@tmpboz\unhbox\hsplit@tmpboy}%
      \unskip
      \unpenalty
  \repeat
}%
\def\hsplit@removehboxes{%
  \setbox\hsplit@tmpboz=\lastbox
  \ifhbox\hsplit@tmpboz
    {\hsplit@removehboxes}% for use local tmpboz
    \unhbox\hsplit@tmpboz
    \unskip % removes \rightskip
  \fi
}%
\fi
